/*
* EP-Tracing System : a tool to trace mountain hike.           .kNNx
* Copyright (C) 2009  Raffaello Brondi                         'KNNNk
*                                                             ,XNNNl
* This program is free software:                             ,XNN0'
* you can redistribute it and/or modify                     ,XNNx.
* it under the terms of the                                'XNNl
* GNU General Public License                              .KNN:                     ..
* as published by                                        .0NNo                    .ONNO.
* the Free Software Foundation,                          xNNN.  :dkd,            .KNNNN;
* either version 3 of the License, or                   :NNNK ;XNNNNNo          ,KNNNX:
* (at your option) any later version.                   xNNNNcXNNNNNNk         cNNNNx.
*                                                       ,XNNNNNNNNNNX'       .xNNNK;
*                                                        .lKNNNNNNNK'       :KNNNo
*                                                          ;kNNNNNKxl;'.'l0NNNk.
* This program is distributed in                          .'  .:xXNNNNNNNNNNNO,
* the hope that it will be useful,                       :NNXd'   ,oONNNNNNk,
* but WITHOUT ANY WARRANTY;                              xNNNNN0c.    .;;,.
* without even the implied warranty of                   kNNNNNNNXd'
* MERCHANTABILITY or                                     xNNNNNNNNNNOc.
* FITNESS FOR A PARTICULAR PURPOSE.                      xNNNNNNNNNNNNXd.
* See the GNU General Public License                     ONNNN0.,o0NNNNNNl
* for more details.                                      0NNNNd    kNNNNNX.
*                                                        KNNNNl   oNNNNNXc
*                                                       .XNNNN:  dNNNNNx.
* You should have received a copy                       .XNNNN; lNNNNO'
* of the GNU General Public License                     .XNNNN; 'dxo'
* along with this program.                              .XNNNN;
* If not, see <http://www.gnu.org/licenses/>.            0NNNN:
*                                                        kNNNNc
*                                                        oNNNNd
*                                                        ;NNNN0
* Contact: rbrondi@gmail.com                             .XNNNN;
*                                                         kNNNNd
*                                                         .coo:.
*/

package EP;

import EP.UI.Messages;
import java.util.Vector;
import javax.microedition.location.Criteria;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationProvider;

public class Utils {

    public static final String HIKE_NAME = "trp";
    public static final String HIKE_TOTAL_DISTANCE = "td";
    public static final String HIKE_TOTAL_TIME = "tt";
    public static final String HIKE_ALTITUDE = "alt";



    /** First value in vector*/
    private static final int X = 0;
    /** Second value in vector*/
    private static final int Y = 1;
    //private static Vector roots = null;
    //private static Object mutex = new Object();

    /*public synchronized static{
    Thread t = new Thread() {

    public void run() {
    synchronized (mutex) {
    Enumeration drives = FileSystemRegistry.listRoots();
    roots = new Vector();
    while (drives.hasMoreElements()) {
    roots.addElement(drives.nextElement());
    }
    }
    }
    };

    t.start();
    }
     */
    /**
     * Creates a String presentation of double value that is formatted to have a
     * certain number of decimals. Formatted output value does not include any
     * rounding rules. Output value is just truncated on the place that is
     * defined by received <i>decimals</i> parameter.
     * 
     * @param value -
     *            double value to be converted.
     * @param decimals -
     *            number of decimals in the String presentation.
     * @return a string representation of the argument.
     */
    public static String formatDouble(double value, int decimals) {
        String doubleStr = "" + value;
        int index = doubleStr.indexOf(".") != -1 ? doubleStr.indexOf(".")
                : doubleStr.indexOf(",");
        // Decimal point can not be found...
        if (index == -1) {
            return doubleStr;
        }
        // Truncate all decimals
        if (decimals == 0) {
            return doubleStr.substring(0, index);
        }

        int len = index + decimals + 1;
        if (len >= doubleStr.length()) {
            len = doubleStr.length();
        }

        double d = Double.parseDouble(doubleStr.substring(0, len));
        return String.valueOf(d);
    }

    /**
     * Checks whether Location API is supported.
     *
     * @return a boolean indicating is Location API supported.
     */
    public static boolean isLocationApiSupported() {
        String version = System.getProperty("microedition.location.version");
        return (version != null && !version.equals("")) ? true : false;
    }

    /**
     * Checks whether File Connection API is supported.
     *
     * @return a boolean indicating is File Connection API supported.
     */
    public static boolean isFCApiSupported() {
        String version = System.getProperty("microedition.io.file.FileConnection.version");
        return (version != null && !version.equals("")) ? true : false;
    }

    /*public synchronized static Vector getRoots() {
    return roots;
    }*/
    public static LocationProvider getLocationProvider(
            Criteria criteria) {
        LocationProvider l = null;
        try {
            l = LocationProvider.getInstance(criteria);
        } catch (LocationException ex) {
            ex.printStackTrace();
            Messages.showProvidersOutOfService();
        }

        return l;
    }

    /*public static float[] rotateVector(double angle, int[] vec) {
    float rotatedVec[] = new float[2];
    rotatedVec[X] = (float) (Math.cos(angle) * vec[X] + Math.sin(angle) * vec[Y]);
    rotatedVec[Y] = (float) (-Math.sin(angle) * vec[X] + Math.cos(angle) * vec[Y]);
    return rotatedVec;
    }*/

    /*public static float[] translateVector(float[] p1 ,float[] delta) {
    float translated[] = new float[2];
    translated[X] = (float) ( * vec[X] + Math.sin(angle) * vec[Y]);
    translated[Y] = (float) (-Math.sin(angle) * vec[X] + Math.cos(angle) * vec[Y]);
    return rotatedVec;
    }*/
    
    public static String[] split(String original, String separator) {
        Vector nodes = new Vector();

        int index = original.indexOf(separator);
        while (index >= 0) {
            nodes.addElement(original.substring(0, index));
            original = original.substring(index + separator.length());
            index = original.indexOf(separator);
        }

        nodes.addElement(original);

        String[] result = new String[nodes.size()];
        if (nodes.size() > 0) {
            for (int loop = 0; loop < nodes.size(); loop++) {
                result[loop] = (String) nodes.elementAt(loop);
                System.out.println(result[loop]);
            }

        }

        return result;
    }
}
